{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "27952ee6",
   "metadata": {},
   "source": [
    "---\n",
    "* 时间:2022/10/31\n",
    "* 课程：python\n",
    "* 记录人：马梓杰\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c320f1c",
   "metadata": {},
   "source": [
    "# 本周知识点\n",
    "* 元祖、集合和字典的基本概念和常见方法\n",
    "\n",
    "> 1. 元组\n",
    "> 2. 集合\n",
    "> 3. 字典"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8814759",
   "metadata": {},
   "source": [
    "## 1. 元组（tuple）\n",
    "> 1. 概念\n",
    ">> 1. 具有和序列数据相同的运算规则：取值和切片\n",
    ">> 2. 表征：（12345，54321，‘hello！’），用圆括号/小括号括起来的，中间依然是逗号隔开的数据形态\n",
    "> 2. 性质：**不可变性**,没有什么增删改的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b9bb50af",
   "metadata": {},
   "outputs": [],
   "source": [
    "product = (\"head first python\",88)#创建了一个元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ee1d64a7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('head first python', 88) <class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "print(product,type(product))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5d8773cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 案例：\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "63bb6673",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1667182891.8086474"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.time()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3e65412b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2022, tm_mon=10, tm_mday=31, tm_hour=10, tm_min=19, tm_sec=46, tm_wday=0, tm_yday=304, tm_isdst=0)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "12145eb0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on struct_time object:\n",
      "\n",
      "class struct_time(builtins.tuple)\n",
      " |  struct_time(iterable=(), /)\n",
      " |  \n",
      " |  The time value as returned by gmtime(), localtime(), and strptime(), and\n",
      " |  accepted by asctime(), mktime() and strftime().  May be considered as a\n",
      " |  sequence of 9 integers.\n",
      " |  \n",
      " |  Note that several fields' values are not the same as those defined by\n",
      " |  the C language standard for struct tm.  For example, the value of the\n",
      " |  field tm_year is the actual year, not year - 1900.  See individual\n",
      " |  fields' descriptions for details.\n",
      " |  \n",
      " |  Method resolution order:\n",
      " |      struct_time\n",
      " |      builtins.tuple\n",
      " |      builtins.object\n",
      " |  \n",
      " |  Methods defined here:\n",
      " |  \n",
      " |  __reduce__(...)\n",
      " |      Helper for pickle.\n",
      " |  \n",
      " |  __repr__(self, /)\n",
      " |      Return repr(self).\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Static methods defined here:\n",
      " |  \n",
      " |  __new__(*args, **kwargs) from builtins.type\n",
      " |      Create and return a new object.  See help(type) for accurate signature.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data descriptors defined here:\n",
      " |  \n",
      " |  tm_gmtoff\n",
      " |      offset from UTC in seconds\n",
      " |  \n",
      " |  tm_hour\n",
      " |      hours, range [0, 23]\n",
      " |  \n",
      " |  tm_isdst\n",
      " |      1 if summer time is in effect, 0 if not, and -1 if unknown\n",
      " |  \n",
      " |  tm_mday\n",
      " |      day of month, range [1, 31]\n",
      " |  \n",
      " |  tm_min\n",
      " |      minutes, range [0, 59]\n",
      " |  \n",
      " |  tm_mon\n",
      " |      month of year, range [1, 12]\n",
      " |  \n",
      " |  tm_sec\n",
      " |      seconds, range [0, 61])\n",
      " |  \n",
      " |  tm_wday\n",
      " |      day of week, range [0, 6], Monday is 0\n",
      " |  \n",
      " |  tm_yday\n",
      " |      day of year, range [1, 366]\n",
      " |  \n",
      " |  tm_year\n",
      " |      year, for example, 1993\n",
      " |  \n",
      " |  tm_zone\n",
      " |      abbreviation of timezone name\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Data and other attributes defined here:\n",
      " |  \n",
      " |  n_fields = 11\n",
      " |  \n",
      " |  n_sequence_fields = 9\n",
      " |  \n",
      " |  n_unnamed_fields = 0\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Methods inherited from builtins.tuple:\n",
      " |  \n",
      " |  __add__(self, value, /)\n",
      " |      Return self+value.\n",
      " |  \n",
      " |  __contains__(self, key, /)\n",
      " |      Return key in self.\n",
      " |  \n",
      " |  __eq__(self, value, /)\n",
      " |      Return self==value.\n",
      " |  \n",
      " |  __ge__(self, value, /)\n",
      " |      Return self>=value.\n",
      " |  \n",
      " |  __getattribute__(self, name, /)\n",
      " |      Return getattr(self, name).\n",
      " |  \n",
      " |  __getitem__(self, key, /)\n",
      " |      Return self[key].\n",
      " |  \n",
      " |  __getnewargs__(self, /)\n",
      " |  \n",
      " |  __gt__(self, value, /)\n",
      " |      Return self>value.\n",
      " |  \n",
      " |  __hash__(self, /)\n",
      " |      Return hash(self).\n",
      " |  \n",
      " |  __iter__(self, /)\n",
      " |      Implement iter(self).\n",
      " |  \n",
      " |  __le__(self, value, /)\n",
      " |      Return self<=value.\n",
      " |  \n",
      " |  __len__(self, /)\n",
      " |      Return len(self).\n",
      " |  \n",
      " |  __lt__(self, value, /)\n",
      " |      Return self<value.\n",
      " |  \n",
      " |  __mul__(self, value, /)\n",
      " |      Return self*value.\n",
      " |  \n",
      " |  __ne__(self, value, /)\n",
      " |      Return self!=value.\n",
      " |  \n",
      " |  __rmul__(self, value, /)\n",
      " |      Return value*self.\n",
      " |  \n",
      " |  count(self, value, /)\n",
      " |      Return number of occurrences of value.\n",
      " |  \n",
      " |  index(self, value, start=0, stop=9223372036854775807, /)\n",
      " |      Return first index of value.\n",
      " |      \n",
      " |      Raises ValueError if the value is not present.\n",
      " |  \n",
      " |  ----------------------------------------------------------------------\n",
      " |  Class methods inherited from builtins.tuple:\n",
      " |  \n",
      " |  __class_getitem__(...) from builtins.type\n",
      " |      See PEP 585\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(time.localtime())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9ac5e2cb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function tuple.index(value, start=0, stop=9223372036854775807, /)>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " product.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "2984fc19",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1800001, 1800002, 1800003)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_seq = 1800001,1800002,1800003\n",
    "name_seq"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a303153",
   "metadata": {},
   "source": [
    "# 2.集合（sets）\n",
    "> 1. 集合的概念\n",
    ">> A：没有重复元素的一个数据结构\n",
    ">> B：形态：{'apple','orange','apple','pear','orange','banana'}\n",
    "> 2. 集合的运算\n",
    ">> 1. 交集运算： &  ，intersection\n",
    ">> 2. 并集运算：|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "9ae93a9a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'banana', 'pear', 'orange', 'apple'} <class 'set'>\n"
     ]
    }
   ],
   "source": [
    "basket = {'apple','orange','apple','pear','orange','banana'}\n",
    "print(basket,type(basket))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "34bc9c3b",
   "metadata": {},
   "outputs": [],
   "source": [
    "舞蹈社 = {180001,180003,180004,180005,180007}\n",
    "合唱团 = {180009,181111,180004,180001}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "11d75500",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(舞蹈社)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "a83bdf8f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(合唱团)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "39d8ad1a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 交集运算和并集运算的实践"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "1efcddfa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{180001, 180004}\n",
      "{180001, 180004}\n"
     ]
    }
   ],
   "source": [
    "#交集：\n",
    "print(舞蹈社&合唱团)\n",
    "print(舞蹈社.intersection(合唱团))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "3a4fb656",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{180001, 180003, 180004, 180005, 180007, 180009, 181111}\n",
      "7\n",
      "{180001, 180003, 180004, 180005, 180007, 180009, 181111}\n",
      "7\n"
     ]
    }
   ],
   "source": [
    "#并集：统计数量\n",
    "print(舞蹈社 | 合唱团)\n",
    "print(len(舞蹈社 | 合唱团))\n",
    "\n",
    "print(舞蹈社.union(合唱团))\n",
    "print(len(舞蹈社.union(合唱团)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43a70b4a",
   "metadata": {},
   "source": [
    "# 3. 字典（dict）\n",
    "> 1. 基本的概念\n",
    ">> 1. mapping types : 映射类型\n",
    ">> 2. key:values ,键：值 对\n",
    ">> 3. 字典的性质\n",
    ">>> 1. 无序性\n",
    ">>> 2. key不可变性\n",
    ">>> 3. key的唯一性\n",
    "> 2. 字典的创建\n",
    "\n",
    "> 3. 常用的方法                                                 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "444c6880",
   "metadata": {},
   "outputs": [],
   "source": [
    "product_list = {\n",
    "\"皮蛋瘦肉粥三件套\": 13,\n",
    "\"雪菜笋丁鸡肉粥三件套\": 10,\n",
    "\"生滚牛肉粥三件套\": 17,\n",
    "\"皮蛋瘦肉粥油条三件套\": 13,\n",
    "\"培根蛋法风烧饼咖啡三件套\": 20,\n",
    "\"大饼卷香饼鸡扒蛋豆浆三件套\": 22,\n",
    "\"芝士猪柳蛋帕尼尼豆浆三件套\": 22,\n",
    "\"肯德基活力全餐周末\": 26,\n",
    "\"芝士猪柳帕尼尼全餐\": 28,\n",
    "\"西瓜冰红茶\": 1000\n",
    "} "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c8ba1f0a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "05f92a78",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e0585526",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
